$(document).on('pageinit', function(){
	var studentId = getCookie('studentId');
	var year = getCookie('year');
	var term = getCookie('term');
	if(!/^\d{10}$/.test(studentId)){
		$('#warningText').text('学号不正确，请返回重新输入');
		$('#warningBtn').click();
	}
	else if(!/^20\d{2}-20\d{2}$/.test(year)){
		$('#warningText').text('学年不正确，请返回重新选择');
		$('#warningBtn').click();
	}
	else if(!/\d{1}/.test(year)){
		$('#warningText').text('学期不正确，请返回重新选择');
		$('#warningBtn').click();
	}
	else{
		if( $('#gradeList li').size() == 1){
			$.ajax({
				url: './php/get_grade.php',
				type: 'GET',
				data: '&xh='+studentId+'&xn='+year+'&xq='+term,
				dataType: "JSON",
				success:
					function(data){
						//console.log(data);
						hideLoader();
						if(data.error){
							$('#warningText').text(data.error);
							$('#warningBtn').click();
						}else{
							$('#name').text(data.name+'同学的成绩');
							for(var i in data.grade){
								// var gua = getCookie(studentId+data.grade[i][5].replace(/-|\(|\)/g, ''));
								// if(gua || year != '2014-2015'){
									$('#gradeList').append('<li class="showPS" data-ps="'+data.grade[i][1]+'">'+data.grade[i][4]+'<span class="ui-li-count">'+data.grade[i][3]+'</span></li>').find("li:last").hide();
								// }else{
									// $('#gradeList').append('<li><a class="showGrade" href="#" data-grade="'+data.grade[i][3]+'" data-xkkh="'+data.grade[i][5].replace(/-|\(|\)/g, '')+'">'+data.grade[i][4]+'<span class="ui-li-count" data-xkkh="'+data.grade[i][5].replace(/-|\(|\)/g, '')+'">刮一刮</span></a></li>').find("li:last").hide();
								// }
								$("#gradeList").find("li:last").slideDown(500); 
								$("#gradeList").listview('refresh');	
							}
							//关闭刮刮乐
							/*
							$('.showGrade').on('tap', function(){
								var grade = $(this).data('grade');
								var xkkh = $(this).data('xkkh');
								$('#title').text('成绩刮刮乐');
								$('#grade').append('<img src="./php/get_num.php?num='+grade+'"></img>');
								var c = document.getElementById("target");
								var ctx = c.getContext("2d");
								ctx.fillStyle = "#666666";
								ctx.fillRect(0, 0, 200, 200);
								var length = 0;
								var mousePos={x:0,y:0}, mousePosOld={x:0,y:0};
								$(document).on('mousemove click tap', function(e){
									if(mousePosOld.x > 0 && mousePosOld.y > 0 && mousePos.x > 0 && mousePos.y > 0
									&& mousePosOld.x < 200 && mousePosOld.y < 200 && mousePos.x < 200 && mousePos.y < 200){
										length +=  Math.sqrt((mousePosOld.x - mousePos.x)*(mousePosOld.x - mousePos.x) + (mousePosOld.y - mousePos.y)*(mousePosOld.y - mousePos.y));
									}
									mousePosOld = mousePos;
									mousePos = getMousePos(c, e);
									// console.log(length);
									// ctx.fillStyle="rgba(255, 255, 255, 1)";
									// ctx.beginPath();
									// ctx.arc(mousePos.x, mousePos.y, 15, 0, Math.PI*2, true);
									// ctx.closePath();
									// ctx.fill();
									ctx.clearRect(mousePos.x, mousePos.y, 20, 20);  
									if(length > 600){
										setCookie(studentId+xkkh, grade);
										ctx.clearRect(0, 0, 200, 200);
										$(document).unbind("mousemove click tap");
									}
								});
								$('#guaBtn').click();
							});
							*/
							$('.showPS').on('tap', function(){
								var grade = $(this).data('ps');
								$('#grade').append('<img src="./php/get_num.php?num=233"></img>');
								$('#title').text('幸运值');
								$('#guaBtn').click();
							});
						}
					}
			});
		}
	}
});

// $(document).on('pagechange', function(){
	// var studentId = getCookie('studentId');
	// $('.ui-li-count').each(function(i, e){
		// var xkkh = studentId+$(this).data('xkkh');
		// var grade = getCookie(xkkh);
		// alert(grade);
		// if(grade)
			// $(e).text(grade);
	// });
// });

$(document).ready(function(){
	showLoader();
});

// function getMousePos(canvas, evt) {   
    // var rect = canvas.getBoundingClientRect();   
    // return {   
        // x: evt.pageX - rect.left,   
        // y: evt.pageY - rect.top   
    // };   
// }